/*
 *  box2dCustom.h
 *  graphicsExample
 *
 *  Created by 真鍋 大度 on 11/04/25.
 *  Copyright 2011 Rhizomatiks. All rights reserved.
 *
 */

#pragma once
#include "ofMain.h"
#include "ofxVectorMath.h"
#include "ofxBox2d.h"
#include "ofxControlPanel.h"
#include "ofxTriangle.h"
#include "ofxDirList.h"
// ------------------------------------------------- a simple extended box2d circle
class CustomParticle : public ofxBox2dCircle {
	
public:
	CustomParticle() {
	}
	ofColor color;
	void draw() {
		float radius = getRadius();
		
		glPushMatrix();
		glTranslatef(getPosition().x, getPosition().y, 0);
		
		ofSetColor(color.r, color.g, color.b);
		//		ofFill();
		ofNoFill();
		ofCircle(0, 0, radius);	
		
		glPopMatrix();
		
	}
};

class CustomRect : public ofxBox2dRect {
public:
	
	
	int whichNumber;
	
	CustomRect(){
		whichNumber = (int)(ofRandom(0,11));
	}
	
	ofColor color;
	float w,h;
	ofTrueTypeFont * font;
	
	void passInTrueTypeFont(ofTrueTypeFont * pfont){
		font = pfont;
	}
	
	
	void draw(){
		string str = ofToString(whichNumber);
		
		//ofRectangle boundingBox = font.getStringBoundingBox(str, 0,0);
		
		//float radius = getRadius();
		float rot = getRotation();
		
		glPushMatrix();
		glTranslatef(getPosition().x, getPosition().y, 0);
		ofSetColor(255, 255, 255);
		//			float centerx = tmpRect.x + tmpRect.width / 2;
		//			float centery = tmpRect.y + tmpRect.height / 2;
		//glTranslatef(w/2, h/2, 0);
		ofRotateZ(rot);
		//glTranslatef(-w/2, -h/2, 0);		
		
		ofRectangle tmpRect;
		tmpRect = font->getStringBoundingBox(str, 0, 0);
		
		
		float scalef =  w / 32.0;
		glScalef(scalef, scalef, 1);
		font->drawString(str,-tmpRect.width/2.0, tmpRect.height/2.0);
		
		//w = tmpRect.width;
		//h = tmpRect.height;					
		
		ofSetColor(color.r, color.g, color.b);
		ofFill();
		
		
		//ofRect(-w/2, -h/2, w, h);	
		glPopMatrix();
		
	}
	
};

class CustomImage : public ofxBox2dRect {
public:
	
	int whichImage;
	int currentImage;
	int numImage;
	CustomImage(){
		currentImage = 0;
	}
	
	ofColor color;
	float w,h;
	vector<ofImage> vimgs;
	
	void passInImgs(vector<ofImage> & _vimgs){
		vimgs = _vimgs;
		whichImage = (int)(ofRandom(0,vimgs.size() - 1));		
		numImage = vimgs.size();
	}
	
	void draw(){
		float rot = getRotation();
		glPushMatrix();
		glTranslatef(getPosition().x, getPosition().y, 0);
		ofSetColor(255, 255, 255);
		ofRotateZ(rot);
		float scalef =  w / 32.0;
		glScalef(scalef, scalef, 1);
		ofSetColor(color.r, color.g, color.b);
		ofFill();
		vimgs[currentImage].draw(-w/2, -h/2, w, h);		
		if (ofGetFrameNum() % 10 == 0) {
			currentImage++;
		}
		if (currentImage > numImage - 1) {
			currentImage = 0;
		}
		glPopMatrix();		
	}
	
};

class Mask{
public:
	Mask(){}
	vector<ofPoint>vertices;
	ofColor color;
	
	bool bFill;
	bool bBox2d;
	void addVertex(float x, float y) {
		vertices.push_back(ofPoint(x, y));
	}	
	
	
	void drawWithColor() {
		if(vertices.size() > 0) {
			
			ofEnableAlphaBlending();
			if (bFill) {
				ofFill();
			}else {
				ofNoFill();
			}
			ofSetColor(color.r, color.g, color.b, color.a);
			ofBeginShape();
			for(int i=0; i<vertices.size(); i++) {
				ofVertex(vertices[i].x, vertices[i].y);
			}
			ofEndShape();
			ofDisableAlphaBlending();
			
			// draw center pos
			//			ofSetColor(0xff0000);			
			//			ofPoint c = getCenter();
			//			ofCircle(c.x, c.y, 4);
			
			return;
		}
	}	
	
};

class CustomLine : public ofxBox2dLine{
public:
	CustomLine(){
		
	}
	vector<ofPoint>vertices;
	ofColor color;
	
	bool bFill;
	bool bBox2d;
	void addVertex(float x, float y) {
		vertices.push_back(ofPoint(x, y));
	}	
	
	
	void drawWithColor() {
		if(body != NULL ) {
			
			ofEnableAlphaBlending();
			if (bFill) {
				ofFill();
			}else {
				ofNoFill();
			}
			ofSetColor(color.r, color.g, color.b, color.a);
			ofBeginShape();
			for(int i=0; i<vertices.size(); i++) {
				ofVertex(vertices[i].x, vertices[i].y);
			}
			ofEndShape();
			ofDisableAlphaBlending();
			
			// draw center pos
			//			ofSetColor(0xff0000);			
			//			ofPoint c = getCenter();
			//			ofCircle(c.x, c.y, 4);
			
			return;
		}
	}	
};

// -------------------------------------------------
class CustomPoly : public ofxBox2dPolygon {
	
public:
	CustomPoly(){
		
	}
	ofColor color;
	bool bFill;
	bool bBox2d;
	
	void drawWithColor() {
		if(body != NULL ) {
			
			ofEnableAlphaBlending();
			if (bFill) {
				ofFill();
			}else {
				ofNoFill();
			}
			ofSetColor(color.r, color.g, color.b, color.a);
			ofBeginShape();
			for(int i=0; i<vertices.size(); i++) {
				ofVertex(vertices[i].x, vertices[i].y);
			}
			ofEndShape();
			ofDisableAlphaBlending();
			
			// draw center pos
			//			ofSetColor(0xff0000);			
			//			ofPoint c = getCenter();
			//			ofCircle(c.x, c.y, 4);
			
			return;			
		}
	}	
	
};